<?php
declare(strict_types=1);

namespace App\Admin\Service\System;

use Newbie\Cache\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CacheMonitorService {

    /**
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function getCacheServerInfo (): array {
        $redis = container()->get(Redis::class);

        $info = $redis->info();

        $keys = $redis->keys('newbie*');

        return [
            'keys'   => array_slice($keys, 0, 100),
            'server' => [
                'version'        => &$info['redis_version'],
                'redis_mode'     => ($info['redis_mode'] === 'standalone') ? '单机' : '集群',
                'run_days'       => &$info['uptime_in_days'],
                'aof_enabled'    => ($info['aof_enabled'] == 0) ? '关闭' : '开启',
                'use_memory'     => &$info['used_memory_human'],
                'port'           => &$info['tcp_port'],
                'clients'        => &$info['connected_clients'],
                'expired_keys'   => &$info['expired_keys'],
                'sys_total_keys' => count($keys)
            ]
        ];
    }

    /**
     * 查看缓存内容
     * @param string $key
     * @return string|null
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function view (string $key): ?string {
        /**@var Redis $redis * */
        $redis = container()->get(Redis::class);
        return $redis->getBase($key);
    }

    /**
     * 删除一个缓存
     * @param string $key
     * @return bool
     */
    public function delete (string $key): bool {
        return container()->get(Redis::class)->delBase($key) > 0;
    }

    /**
     * 清空所有缓存
     * @return bool
     */
    public function clear (): bool {
        return container()->get(Redis::class)->flushDB();
    }

}